package jpa.repository;

import jpa.entity.User;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.jpa.repository.query.Procedure;
import org.springframework.data.querydsl.QueryDslPredicateExecutor;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by barton on 16-5-9.
 */
public interface UserRepository extends CrudRepository<User, Long>,
        PagingAndSortingRepository<User, Long>, QueryDslPredicateExecutor<User> {
    Long findByFirstName(String firstName);

    //注意分区大小写,在mysql中默认对表名也是区分大小写的
    @Query(value = "select first_name from t_user where last_name = ?1", nativeQuery = true)
    String findByLastName(String lastName);

    @Query("select u from User u where u.firstName=:firstname or u.lastName=:lastname")
    List<User> findByLastNameOrFirstName(@Param("lastname") String lastname, @Param("firstname")
            String firstname);

//    @Query("select u.id,u.firstName,u.lastName from #{#entityName} u where u.id = ?1")
//    List<User> findAllById(Long id);

    @Modifying
    @Transactional
    @Query("update User set firstName = ?1 where id= ?2")
    int updateFixedFirstName(String firstName, Long id);

    /**
     * 要测试此存储过程 请先执行该类所在包下的plus1inout.sql.创建mysql的存储过程.
     * @param arg
     * @return
     */
    @Procedure("plus1inout")
    Integer explicitlyNamedPlus1inout(Integer arg);
}
